﻿using Microsoft.Xna.Framework;
#if WINDOWS
using System.Windows.Forms;
#endif
using Brain.Editor;
using System.Collections.Generic;

namespace Brain.Editor
{
    public class DefaultPickingManager2D : IPickingManager2D
    {
        public bool ShouldUpdate { get; set; }

        public ISprite SelectedSprite { get; set; }

        public bool Manipulating { get; set; }

        public ManipulationMode2D Mode { get; set; }

        public BoxScale Scale;

        public List<ISprite> CustomSprites;
        public bool ReadFromRenderer2D = true;

        public DefaultPickingManager2D(
#if WINDOWS
Control control
#endif
)
        {
            ShouldUpdate = true;

            Scale = new BoxScale(
#if WINDOWS
control,
#endif
 this);

            Scale.SetTarget += new SetRectangle(Scale_SetTarget);
        }
#if WINDOWS
        public DefaultPickingManager2D()
        {
            ShouldUpdate = true;

            Scale = new BoxScale(this);

            Scale.SetTarget += new SetRectangle(Scale_SetTarget);
        }
#endif

        void Scale_SetTarget(Rectangle r)
        {
            if (SelectedSprite != null)
            {
                SelectedSprite.BoundsRectangle = r;
            }
        }

        private Vector2 StartPos;
        public void Update()
        {
            if (Engine.Instance == null || Engine.Instance.RenderManager2D == null)
            {
                return;
            }

            if (Engine.Instance.CameraManager != null &&
                Engine.Instance.CameraManager.Camera2D != null)
            {
                if (Engine.Instance.InputManager.IsMouseMiddleDown())
                {
                    Vector2 mouseDif = Engine.Instance.InputManager.GetMouseDifference();
                    (Engine.Instance.CameraManager.Camera2D as Camera2D).Position += mouseDif;
                }
            }

            Vector2 mouse = Engine.Instance.InputManager.GetMousePosition();
            if (Engine.Instance.CameraManager != null &&
                Engine.Instance.CameraManager.Camera2D != null)
            {
                Matrix m = Matrix.Invert(Engine.Instance.CameraManager.Camera2D.World);
                mouse = Vector2.Transform(mouse, m);
            }

            Rectangle mouseRect = new Rectangle((int)mouse.X, (int)mouse.Y, 1, 1);

            if (!Manipulating)
            {
                if (Engine.Instance.InputManager.IsMouseLeftClick())
                {
                    bool proceed = true;
                    if (SelectedSprite != null)
                    {
                        if (SelectedSprite.BoundsRectangle.Intersects(mouseRect))
                        {
                            StartPos = mouse - new Vector2(SelectedSprite.BoundsRectangle.X, SelectedSprite.BoundsRectangle.Y);
                            proceed = false;
                            // Manipulate
                            Manipulating = true;
                        }
                    }
                    if (proceed)
                    {
                        if (ReadFromRenderer2D)
                        {
                            for (int i = 0; i < Engine.Instance.RenderManager2D.Editor_Count(); i++)
                            {
                                ISprite sprite = Engine.Instance.RenderManager2D.Editor_GetSprite(i);
                                if (mouseRect.Intersects(sprite.BoundsRectangle))
                                {
                                    this.SelectedSprite = sprite;
                                    break;
                                }
                            }
                        }
                        if (this.CustomSprites != null)
                        {
                            for (int i = 0; i < CustomSprites.Count; i++)
                            {
                                ISprite sprite = CustomSprites[i];
                                if (mouseRect.Intersects(sprite.BoundsRectangle))
                                {
                                    this.SelectedSprite = sprite;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                if (Engine.Instance.InputManager.IsMouseLeftUp())
                {
                    Manipulating = false;
                }

                switch (Mode)
                {
                    case ManipulationMode2D.Translation:
                        {
                            Vector2 add = mouse - StartPos;
                            SelectedSprite.BoundsRectangle =
                                new Rectangle((int)add.X,
                                    (int)add.Y,
                                    SelectedSprite.BoundsRectangle.Width,
                                    SelectedSprite.BoundsRectangle.Height);
                        }
                        break;
                    case ManipulationMode2D.Scale:
                        {
                            Vector2 add = mouse;
                            SelectedSprite.BoundsRectangle =
                                new Rectangle(
                                    SelectedSprite.BoundsRectangle.X,
                                    SelectedSprite.BoundsRectangle.Y,
                                    SelectedSprite.BoundsRectangle.Width,
                                    SelectedSprite.BoundsRectangle.Height);
                        }
                        break;
                    case ManipulationMode2D.Rotation:
                        {
                        }
                        break;
                    default:
                        {
                        }
                        break;
                }
            }

            if (SelectedSprite != null)
            {
                Scale.Target = SelectedSprite.BoundsRectangle;
                Scale.Draw();
            }
        }


    }
}
